<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加新用户</title>
<link href="../css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://pic2.58.com/n/tuan/js/jquery-1.4.2.min.js"></script>
</head>
<body>
<script >
		var functions=",";
		var roles="";
		var rolefuncs="";
		var opers=",";
		
		
		function checkAll(){
		 	  var flag=false;
			  
			  var username = $("#username").val();
			  var realname = $("#realname").val();
			  var department = $("#department").val();
			  
			  if(username == null || username == ""){
			  	alert("请填写用户名称！");
			  }else if(realname == null || realname == ""){
			  	alert("请填写真实姓名！");
			  }else if(department == null || department == ""){
			  	alert("请填写所在部门！");
			  }else if(functions == null || functions == ""){
			  	alert("请添加权限!");
			  }
			  else{
			 	  flag=true;
			  }
			  return  flag;
		 }
		 
		 
		 
		 function checkSelectAll(){
		 	$(".selectAll").each(function(){
		 		var f=true;
		 		$(this).nextAll(':checkbox').each(function(){
    				if($(this).attr("checked")==false)
    					f=false;
  				});
		 		$(this).attr("checked",f);
		 	});
		 }
		 
		 
		$(function(){
		 	roles="";
		 	rolefuncs="";
		 	$("#role input:checkbox[checked]").each(function(){
		 		var id=$(this).attr("id");
		 		var f=$(this).attr("func");
		 			rolefuncs+=f;
		 		if(roles=="")
		 			roles+=id;
		 		else
		 			roles+=","+id;	
		 	});

			var funcids=functions.split(",");
			for (var i = 0; i < funcids.length; i++) {
		 		var menuid="#function_"+funcids[i];
       			$(menuid).attr("checked",true);
    		};
		 	var menusids = rolefuncs.split(",");
		 	for (var i = 0; i < menusids.length; i++) {
		 		var menuid="#function_"+menusids[i];
       			$(menuid).attr("checked",true);
       			$(menuid).attr("disabled",true);
    		}
		 });
		 
		
		
		 
		 $(function(){
		 
			 $("#role :checkbox").click(function() {
				if ($(this).attr("checked") == false){
					$(this).removeAttr("checked");
				}else{
					$(this).attr("checked", true);
					
				}
				
				$(".menupri input:checkbox[disabled]").each(function() {
						$(this).removeAttr("disabled");
						$(this).removeAttr("checked");
				});	
				
				
				$(".operlist input:checkbox[disabled]").each(function() {
						$(this).removeAttr("disabled");
						$(this).removeAttr("checked");
				});	

				roles = "";
				rolefuncs = "";
				roleOpers = "";
				$("#role input:checkbox[checked]").each(function() {
							var id = $(this).attr("id");
							var f = $(this).attr("func");
							var oper = $(this).attr("opers");
							rolefuncs += f;
							roleOpers += oper;
							if (roles == ""){
								roles += id;
							}else{
								roles += "," + id;
							}
				});
				
				var menusids = rolefuncs.split(",");
				for (var i = 0; i < menusids.length; i++) {
					var menuid = "#function_" + menusids[i];
					var a = "," + menusids[i] + ",";
					if (functions.indexOf(a) >= 0) {
						functions = functions.replace(eval("/" + a + "/g"), ",");
					}
					$(menuid).attr("checked", true);
					$(menuid).attr("disabled", true);
				};
				
				var operids = roleOpers.split(",");
				for (var i = 0; i < operids.length; i++) {
					var operid = "#opers_" + operids[i];
					var a = "," + operids[i] + ",";
					if (opers.indexOf(a) >= 0) {
						opers = opers.replace(eval("/" + a + "/g"), ",");
					}
					$(operid).attr("checked", true);
					$(operid).attr("disabled", true);
				};
				
				checkSelectAll();
			});
			 
			
			$(".menupri input:checkbox.selectOne").click(function (){
			 	var id= $(this).attr("value");
			 	var a = ","+id+",";
			 	if($(this).attr("disabled")==false){
			 	if($(this).attr("checked")==false){
			 		$(this).prevAll(".selectAll").attr("checked",false);
			 		functions=functions.replace(eval("/"+a+"/g"),",");
			 	}
			 	else{
			 		if(functions.indexOf(a) < 0){
						functions=functions+id+",";
					}
					var f=true;
					$(this).prevAll(".selectAll").nextAll(":checkbox").each(function(){
	    				if($(this).attr("checked")==false)
	    					f=false;
	  				});
	  				$(this).prevAll(".selectAll").attr("checked",f);
			 	}	
			  }
			 });
			 
			 
			 $(".menupri input:checkbox.selectAll").click(function (){
			 		var flag=$(this).attr("checked");
			 		$(this).nextAll(':checkbox').attr("checked",flag);
			 		$(this).nextAll(':checkbox').each(function(){
	    				var id= $(this).attr("value");
	    				var a = ","+id+",";
	    				var tmp=rolefuncs+",";
	    				if(flag && functions.indexOf(a) < 0 && tmp.indexOf(a) < 0)
							functions=functions+id+",";
						else if(flag==false){
							functions=functions.replace(eval("/"+a+"/g"),",");
							if(tmp.indexOf(a) >= 0)
								$(this).attr("checked",true);
						}
	  			});
			 });
			 
			 
			 $(".operlist input:checkbox.selectOne").click(function (){
			 	var id= $(this).attr("value");
			 	var a = ","+id+",";
			 	if($(this).attr("disabled")==false){
			 	if($(this).attr("checked")==false){
			 		$(this).prevAll(".selectAll").attr("checked",false);
			 		opers=opers.replace(eval("/"+a+"/g"),",");
			 	}
			 	else{
			 		if(opers.indexOf(a) < 0){
						opers=opers+id+",";
					}
					var f=true;
					$(this).prevAll(".selectAll").nextAll(":checkbox").each(function(){
	    				if($(this).attr("checked")==false)
	    					f=false;
	  				});
	  				$(this).prevAll(".selectAll").attr("checked",f);
			 	}
			  }
			 });
			 
			 
			 $(".operlist input:checkbox.selectAll").click(function (){
			 		var flag=$(this).attr("checked");
			 		$(this).nextAll(':checkbox').attr("checked",flag);
			 		$(this).nextAll(':checkbox').each(function(){
	    				var id= $(this).attr("value");
	    				var a = ","+id+",";
	    				if(flag && opers.indexOf(a) < 0)
							opers=opers+id+",";
						else if(flag==false){
							opers=opers.replace(eval("/"+a+"/g"),",");
						}
	  				});
			 });
		 
		});
		 
		
		 
		$(function(){$("#toadd").click(function(){ 
		 		if(checkAll()){
    		 		jQuery.ajax({
    						url:"/usermanager/toadduser",
    						type:"post",
    						async:true,
    						data:{"username":$("#username").val(),"realname":$("#realname").val(),
    						"department":$("#department").val(),"roles":roles,"functions":functions,"opers":opers},
    						success:function(data){
    							if(data == "success"){
    								alert("添加成功！");
    							}else if(data == "has"){
									alert("用户名称已存在！");
								}else{
    								alert("添加失败！");
    							}
    						}
    				});
				}
			 })
		 });
</script>
<div class="lr_both">
<div class="right_conts">
  <div class="mg_tt">
    <div class="right_search">
      <div class="rs_title_bg">
        <h2 class="rs_title">用户添加</h2>
      </div>
	  
    </div>
	</br>
	</br>
	<form >
    <div class="table_div" align="center">
	  <input type="hidden" id="bcs" name="bcs"/>
	  <input type="hidden" id="isbusiness" name="isbusiness"/>
      <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
          <th align="center" colspan="2">用户基本信息</th>
		</tr>
		
        <tr>
           <td align="center" >用户名称(与OA名保持一致)</td>
		  <td align="center"  ><input type="text"  name="username" id="username"/></td>
		
        </tr>

		
		  <tr>
           <td align="center" >真实姓名</td>
		  <td align="center" ><input type="text" name="realname" id="realname" /></td>
        </tr>
		
		  <tr>
           <td align="center">所在部门</td>
		  <td align="center" ><input type="text" name="department" id="department" /></td>
        </tr>
	 </table>
	 </br>
	 <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
           <th align="center" colspan="2">角色</th>
		  </th>
		</tr>
		
		
		 <tr>
			 <td align="left" id="role">
				#foreach($r in $roleList)
                    <input type="checkbox" name="$r.id" id="$r.id" value="$r.rolename" #if($tools.checkPublic($r.rolename)) checked disabled #end opers="$roleOperMap.get($r.id)" func="$roleMap.get($r.id)">&nbsp $r.rolename</input>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				#end
			 </td>
		</tr>
		
	</table>
		</br>
	 <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
           <th align="center" colspan="2">菜单权限</th>
		  </th>
		</tr>
		#foreach($key in $menuMap.keySet())
		 <tr class="menupri">
			#set($cate1 = $key.split('-')[0])
			#set($cate2 = $key.split('-')[1])
             <td align="center" width ="20%"><span style="color:red">$cate1</span> </br> <span style="color:blue">$cate2</span></td>
			 <td align="left" >
			 	<input type="checkbox" class="selectAll">&nbsp 全选 </input>&nbsp;&nbsp;
				#foreach($menu in $menuMap.get($key))
                    <input type="checkbox" class="selectOne" name="function_$menu.id" id="function_$menu.id" value="$menu.id">&nbsp $menu.name</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				#end
			 </td>
			 
		</tr>
		#end
      </table>
	  </br>
  
  	 <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
           <th align="center" colspan="2">数据操作细粒度权限</th>
		  </th>
		</tr>
		
		#foreach($key in $operMenuMap.keySet())
		 <tr class="operlist">
			 #set($cate1 = $key.split('-')[0])
		  	 #set($cate2 = $key.split('-')[1])
             <td align="center" width ="20%"><span style="color:red">$cate1</span> </br> <span style="color:blue">$cate2</span></td>
			 <td align="left" >
			 	<input type="checkbox" class="selectAll">&nbsp 全选 </input>&nbsp;&nbsp;
				#foreach($oper in $operMenuMap.get($key))
                    <input type="checkbox" class="selectOne" name="opers_$oper.id" id="opers_$oper.id" value="$oper.id">&nbsp $oper.name</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				#end
			 </td>
			 
		</tr>
		#end
      </table>
	  
	  
	  <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
          <th align="center" colspan="2">
            <input type="button"  id="toadd" name="toadd"  value="保存"/>
			&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;
			<input type="reset"    value="重置"/>
		  </th>
		</tr>
      </table>
    </div>
	</form>
  </div>

</div>
</div>
</body>
</html>
